package com.aizuda.boot.modules.business.employee.service.impl;

import com.aizuda.boot.modules.business.contract.dto.UserInfo;
import com.aizuda.boot.modules.business.contract.util.AuthUtil;
import com.aizuda.boot.modules.business.department.domain.response.EmployeePermissionVO;
import com.aizuda.boot.modules.business.employee.domain.request.EmployeeQueryVO;
import com.aizuda.boot.modules.business.employee.domain.response.EmployeeQueryShowVO;
import com.aizuda.boot.modules.business.permission.domain.entity.EmployeeEntity;
import com.aizuda.boot.modules.business.employee.mapper.EmployeeMapper;
import com.aizuda.boot.modules.business.employee.service.EmployeeService;
import com.aizuda.boot.modules.system.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 用户服务类实现
 * @author duanyao
 * @date 2021/12/13
 */
@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, EmployeeEntity> implements EmployeeService {


    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private ISysUserService sysUserService;


    @Override
    public List<EmployeeQueryShowVO> queryEmployee(EmployeeQueryVO query) {
        // 如果查询条件为空，返回空列表
        if (query == null || (query.getMsg() == null || query.getMsg().trim().isEmpty())) {
            return new ArrayList<>();
        }


        // 预处理查询条件
        String searchMsg = query.getMsg().trim();

        // 创建查询对象副本以避免修改原始对象
        EmployeeQueryVO processedQuery = new EmployeeQueryVO();
        processedQuery.setMsg(searchMsg);

        try {
            // 调用 mapper 查询
            List<EmployeeQueryShowVO> results = this.baseMapper.queryEmployee(processedQuery);

            // 如果是纯英文查询且结果较少，可以尝试更宽松的匹配
            if (isEnglishOnly(searchMsg) && (results == null || results.size() < 5)) {
                // 可以在这里添加额外的匹配逻辑
                log.debug("英文查询结果较少，搜索条件: {}, 结果数量: {}", searchMsg, results != null ? results.size() : 0);
            }

            return results != null ? results : new ArrayList<>();

        } catch (Exception e) {
            log.error("查询员工失败，查询条件: {}", query, e);
            return new ArrayList<>();
        }
    }

    /**
     * 判断字符串是否只包含英文字母
     */
    private boolean isEnglishOnly(String str) {
        return str != null && str.matches("^[a-zA-Z]+$");
    }

    @Override
    public List<EmployeeQueryShowVO> queryEmployeeByIds(List<Long> employeeIds) {
        // 验证参数
        if (employeeIds == null || employeeIds.isEmpty()) {
            log.warn("员工ID列表为空，返回空结果");
            return new ArrayList<>();
        }

        try {
            // 调用 mapper 查询
            List<EmployeeQueryShowVO> results = this.baseMapper.queryEmployeeByIds(employeeIds);
            
            if (results == null || results.isEmpty()) {
                log.info("未找到指定员工ID对应的员工信息，员工ID列表：{}", employeeIds);
                return new ArrayList<>();
            }
            
            log.info("成功查询到{}名员工信息，输入员工ID数量：{}", results.size(), employeeIds.size());
            return results;
            
        } catch (Exception e) {
            log.error("根据员工ID列表查询员工信息时发生异常，员工ID列表：{}", employeeIds, e);
            return new ArrayList<>();
        }
    }

    @Override
    public EmployeePermissionVO queryPermission(Long employeeId) {
        return employeeMapper.queryPermission(employeeId);
    }

    @Override
    public Integer getTenantId(Long employeeId) {

       return employeeMapper.getTenantId(employeeId);
    }
}
